using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;

public class Test_MultPassAniGraph : MonoBehaviour
{
    public Animator animator;
    public AnimationClip fullBodyClip;
    public AnimationClip upperBodyClip;
    public AvatarMask upperBodyMask; // 需要你在Editor中创建这个Mask，仅勾选上半身骨骼

    private PlayableGraph playableGraph;

    void SStart()
    {
        playableGraph = PlayableGraph.Create("UpperBodyLayerGraph");
        var output = AnimationPlayableOutput.Create(playableGraph, "Animation", animator);

        // 创建 Layer Mixer
        var mixer = AnimationLayerMixerPlayable.Create(playableGraph, 2);
        output.SetSourcePlayable(mixer);

        // Layer 0: 全身动画（基础层）
        var fullBodyPlayable = AnimationClipPlayable.Create(playableGraph, fullBodyClip);
        playableGraph.Connect(fullBodyPlayable, 0, mixer, 0);
        mixer.SetInputWeight(0, 1.0f); // 全身层全权重

        // Layer 1: 上半身动画，带 AvatarMask
        var upperBodyPlayable = AnimationClipPlayable.Create(playableGraph, upperBodyClip);
        playableGraph.Connect(upperBodyPlayable, 0, mixer, 1);
        mixer.SetInputWeight(1, 1.0f); // 设置权重
        mixer.SetLayerAdditive(1, false); // 非加成混合模式
        mixer.SetLayerMaskFromAvatarMask(1, upperBodyMask); // 关键操作：遮罩设置 --就是说可以支持对单个端口设置Layer层级

        playableGraph.Play();

        //就是说以牵手奔跑为例, handsFree = HandsIK , 由TouchInfo来决定活动部位, 其他取反,

        //也就是说有两种播放模式, 单播手的动画 , 单不播手的动画 , 

        // Play(clip , status)  PlaySingle(clip , 播放部位 , status) PlayInControl (clip , control部位 status)

    }
    public VS_MulitPassAnimationGraph mulitPassAnimationGraph;
    void Start()
    {
        mulitPassAnimationGraph = new(gameObject);
        mulitPassAnimationGraph.SetDefaultAnimation(fullBodyClip);
    }
    [Button]
    void PlaySingle()
    {
        mulitPassAnimationGraph.PlaySingle(upperBodyClip, VAvatarMaskID.RightHand);
    }

    [Button]
    void PlayInControl()
    {
        mulitPassAnimationGraph.PlayInControl(upperBodyClip,VAvatarMaskID.RightHand);
    }

    void OnDisable()
    {
        mulitPassAnimationGraph.Destroy();
    }

}
